﻿#include "OperPhysicsPropBeam.h"
#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"
#include "GUIDialog/GUIPhyscisDialog/PhysicsDialogBeam.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsPhysics.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKPhyscisPropertyBeam.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsAbsPhysicsProp.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsAbsMaterial.h"

#include "updateBrowserTable.h"
#include "GUIWidget/AttributeSetDialog.h"
namespace ModelOper
{
    bool OperPhysicsPropBeam::execGUI()
    {
        //按钮点击事件触发
        if (_emitter == nullptr) return false;
        const QString name = _emitter->objectName();
        if (name == "actionBeams")
            createPhysicsProp();
        return false;
    }

    bool OperPhysicsPropBeam::execProfession()
    {
        return false;
    }

    void OperPhysicsPropBeam::createPhysicsProp()
    {
        //弹出对话框
        QWidget* mw = FITKAPP->getGlobalData()->getMainWindow();
        GUI::PhysicsDialogBeam dlg(GUI::PhysicsDlgExecType::Create, -1, mw);
        //更新列表
        if (QDialog::Accepted == dlg.exec())
            ModelOper::updateBrowserTable();
    }

    void OperPhysicsPropBeam::duplicatePhysicsProp(const int propID)
    {
        QWidget* mw = FITKAPP->getGlobalData()->getMainWindow();
        //编辑对话框
        GUI::PhysicsDialogBeam dlg(GUI::PhysicsDlgExecType::Duplicate, propID, mw);
        //更新列表
        if (QDialog::Accepted == dlg.exec())
            ModelOper::updateBrowserTable();
    }

    void OperPhysicsPropBeam::editPhysicsProp(const int propID)
    {
        QWidget* mw = FITKAPP->getGlobalData()->getMainWindow();
        //编辑对话框
        GUI::PhysicsDialogBeam dlg(GUI::PhysicsDlgExecType::Edit, propID, mw);
        //更新列表
        if (QDialog::Accepted == dlg.exec())
            ModelOper::updateBrowserTable();
    }

    void OperPhysicsPropBeam::removePhysicsProp(const int propID)
    {
        //获取管理器
        Acoustics::FITKAcousticsPhysics* phys = Acoustics::FITKAcousticsPhysics::GetAcousticsPhysicsFromFrame();
        if (phys == nullptr) return;
        Acoustics::FITKAcousticsPhysicsPropManager* propMgr = phys->getPhysicsPropManager();
        if (propMgr == nullptr) return;
        //移除并更新列表
        propMgr->removeDataByID(propID);
        ModelOper::updateBrowserTable();
    }

    void OperPhysicsPropBeam::setAttribute(const QString & name, const QString & text, const int attrIndex, const QList<int>& propID)
    {
        QWidget* mw = FITKAPP->getGlobalData()->getMainWindow();
        Acoustics::FITKAcousticsPhysics* phys = Acoustics::FITKAcousticsPhysics::GetAcousticsPhysicsFromFrame();
        if (phys == nullptr) return;
        Acoustics::FITKAcousticsPhysicsPropManager* propMgr = phys->getPhysicsPropManager();
        Acoustics::FITKAcousticsMaterialManager* matMgr = phys->getMaterialManager();
        if (matMgr == nullptr) return;
        //设置对话框
        GUI::AttributeSetDialog dlg(2, mw);
        dlg.setTextLabel(name);
        
        QList<Acoustics::FITKAcousticsAbsMaterial*>matList = matMgr->getAcousticsMaterials(Acoustics::FITKAcousticsAbsMaterial::ISOSolid);
        QList<QString> list;
        for (auto value : matList) {
            list += value->getDataObjectName();
        }
        dlg.setDefaultValueQString(list);
        if (dlg.exec() != QDialog::Accepted) return;

        QString mat = dlg.getValueQString();//需要getText
        int matID = matMgr->getDataByName(mat)->getDataObjectID();
        for (int id : propID)
        {
            Acoustics::FITKPhyscisPropertyBeam* prop = dynamic_cast
                <Acoustics::FITKPhyscisPropertyBeam*>(propMgr->getDataByID(id));
            if (prop == nullptr) continue;
            //不同列设置不同参数
            prop->setMaterialID(matID);
        }
        ModelOper::updateBrowserTable();
    }

}
